<template>
	<view class="template-blogger tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>

		
		  
		<!-- 立体头像-->
		<view class='cube' :style="'background-image: url(https://resource.tuniaokj.com/images/blogger/bg_image_1.jpg);'">
		
			<view class='tn-text-center tn-margin-top-lg'>
				<view class="tn-padding tn-text-bold tn-text-lg">{{ userInfo.username }}</view>
				<view class="tn-padding-bottom-xl tn-text-lg">{{ userInfo.desc || "我有点懒，不想写"}}</view>
			</view>
		</view>
		
		<swiper class="card-swiper"  :circular="true" :autoplay="true" duration="500" interval="8000" :previous-margin="5" :next-margin="5"> 
			<swiper-item v-for="(item,index) in userInfo.avatar" :key="index" :class="cardCur==index?'cur':''"  @click="previewPhoto(index)">
			  <view class="swiper-item image-banner" >
				<image  :src="item" mode="aspectFill" ></image>
			  </view>
			</swiper-item>
		  </swiper>


		<!-- 内容 -->
		<view class="tn-flex tn-flex-direction-column tn-margin-top-sm tn-margin-bottom">


			<!-- 边距间隔 -->
			<view class="tn-strip-bottom"></view>

			<!-- 图文信息 -->
			<block v-for="(item,index) in content" :key="index">
				<view class="blogger__item">
					<view class="blogger__author tn-flex tn-flex-row-between tn-flex-col-center">
						<view class="justify__author__info">
							<view class="tn-flex tn-flex-row-center">
								<view class="tn-flex tn-flex-row-center tn-flex-col-center">
									<view class="">
										<tn-avatar class="" shape="circle" :src="item.headUrl" size="lg">
										</tn-avatar>
									</view>
									<view class="tn-padding-right tn-text-ellipsis">
										<view class="tn-padding-right tn-padding-left-sm tn-text-bold tn-text-lg">
											{{ item.userName }}</view>
										<view
											class="tn-padding-right tn-padding-left-sm tn-padding-top-xs tn-color-gray">
											{{ item.createTime }}</view>
									</view>
								</view>
							</view>
						</view>
						<view v-if="canDel" @click="deleteArticle(item, index)" class="blogger__author__btn justify-content-item tn-flex-col-center tn-flex-row-center">
							<text class="tn-icon-delete tn-color-black tn-text-bold tn-text-xxl"></text>
						</view>
					</view>

					<view
						class="blogger__desc tn-margin-top-sm tn-margin-bottom-sm tn-text-justify tn-flex-col-center tn-flex-row-left"
						@click="tn('/circlePages/details')">
						<view v-for="(label_item,label_index) in item.label" :key="label_index"
							class="blogger__desc__label tn-float-left tn-margin-right tn-bg-gray--light tn-round tn-text-sm tn-text-bold">
							<text class="blogger__desc__label--prefix">#</text>
							<text class="tn-text-df">{{ label_item }}</text>
						</view>
						<!-- 不用限制长度了，因为发布的时候限制长度了-->
						<text v-if="!item.label || item.label.length < 4"
							class="blogger__desc__content tn-flex-1 tn-text-justify tn-text-df">{{ item.desc }}</text>
					</view>

					<!-- 内容太多疲劳了-->
					<view v-if="item.content" class="blogger__content" :id="`blogger__content--${index}`">
						<view class="blogger__content__data" :style="{
                height: `${item.contentContainerInit ? (!item.showAllContent ? contentHideShowHeight : item.contentContainerHeight) + 'px' : 'auto'}`
              }">
							{{ item.content }}
						</view>
						<view v-if="item.hideContent" class="blogger__content__status"
							@tap="switchContentShowStatus(index)">{{ item.showAllContent ? '收起' : '全文'}}</view>
					</view>

					<block v-if="item.imageArray">
						<view v-if="[1,2,4].indexOf(item.imageArray.length) != -1" class="tn-padding-top-xs"
							@click="tn('/circlePages/details?id='+item.id)">
							<image v-for="(image_item,image_index) in item.imageArray" :key="image_index"
								class="blogger__main-image" :class="{
                  'blogger__main-image--1 tn-margin-bottom-sm': item.imageArray.length === 1,
                  'blogger__main-image--2 tn-margin-right-sm tn-margin-bottom-sm': item.imageArray.length === 2 || item.imageArray.length === 4
                }" :src="image_item" mode="aspectFill"></image>
						</view>
						<view v-else class="tn-padding-top-xs" @click="tn('/circlePages/details?id='+item.id)">
							<tn-grid hoverClass="none" :col="3">
								<block v-for="(image_item,image_index) in item.imageArray" :key="image_index">
									<!-- #ifndef MP-WEIXIN -->
									<tn-grid-item style="width: 30%;margin: 10rpx;">
										<image class="blogger__main-image blogger__main-image--3" :src="image_item"
											mode="aspectFill"  ></image>
									</tn-grid-item>
									<!-- #endif-->
									<!-- #ifdef MP-WEIXIN -->
									<tn-grid-item style="width: 30%;margin: 10rpx;">
										<image class="blogger__main-image blogger__main-image--3" :src="image_item"
											mode="aspectFill"></image>
									</tn-grid-item>
									<!-- #endif-->
								</block>
							</tn-grid>
						</view>
					</block>

					<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin-top-xs">
						<view class="justify-content-item tn-color-gray tn-text-center">
							<view class="">
								<text class="blogger__count-icon tn-icon-message"></text>
								<text class="tn-padding-right">{{ item.commentNum }}</text>
								<text class="blogger__count-icon tn-icon-like"></text>
								<text class="">{{ item.likeNum }}</text>
							</view>
						</view>
						<!-- <view class="justify-content-item tn-flex tn-flex-col-center">
              <view style="margin-right: 10rpx;margin-left: 20rpx;">
                <tn-avatar-group :lists="item.imgList" size="sm"></tn-avatar-group>
              </view>
              <text class="tn-color-gray">{{ item.viewUser.viewUserCount }}人</text>
            </view> -->
					</view>
				</view>

				<!-- 边距间隔 -->
				<view class="tn-strip-bottom" v-if="index != content.length - 1"></view>
			</block>


		</view>
	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	import request from '@/common/request.js';
	export default {
		name: 'TemplateBlogger',
		mixins: [template_page_mixin],
		data() {
			return {
				// 内容默认隐藏显示的高度
				contentHideShowHeight: 0,
				userInfo: {
					avatar: [],
					username: '',
					desc: '我不想写简介了..'
				},
				userId: 0,
				canDel:false,
				user: {},
				content: [],
				adAutoplay: false,
				cardCur:0,
			}
		},
		computed: {},

		onLoad(e) {
			// 使用 Object.keys() 获取对象中所有键名组成的数组
			let id = Object.keys(e)[0];
			this.userId = e.id || id || uni.getStorageSync("userInfo")['id']
			this.selectPersonList();
			this.initContentData()
			
			var testId = e.id || id || 0
			
			if(!testId && uni.getStorageSync("userInfo")['id']){
				this.canDel = true;
			}
			this.contentHideShowHeight = uni.upx2px(56) * 3
		},
		onReady() {
			this.$nextTick(() => {
				this.getContentRectInfo()
			})
		},
		onShow() {
			this.adAutoplay = true
			this.checkLogin()
		},
		onHide() {
			this.adAutoplay = false
		},
		methods: {
			previewPhoto(url) {
			  uni.previewImage({
				current: url, // 当前显示图片的链接（必填）
				urls: this.userInfo.avatar, // 需要预览的图片链接列表（必填）
				success(res) {
				  //console.log('预览成功');
				},
				fail(err) {
				  //console.error('预览失败', err);
				}
			  });
			},
			// 删除
			deleteArticle(item, index){
				let that = this;
				uni.showModal({
					title: '确认框',
					content: '确认删除本条动态吗?',
					success: function (res) {
						if (res.confirm) {
							// 确认删除
							console.log("删除编号为 "+ item.id + " 的数据")
							
							request.delete("friend/wx/circle/" + item.id).then(res => {
								let _data = res[1]['data']
								if (_data.code == 20000) {
									console.log("删除成功")
									that.$delete(that.content, index)
								}else{
									console.log("删除失败")
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			selectPersonList() {
				request.get("friend/wx/circle/personList/" + this.userId, {}).then(res => {
					let re = res[1]['data']
					if (re.code == 20000) {
						
						
						console.log(re.data)
						
						this.user.desc = this.userInfo.desc = re.data.selfIntroduction
						this.userInfo.username = re.data.name
						
						console.log(this.userInfo)
						
						if (re.data.imageList && re.data.imageList.length > 0) {
							let _cc = [];
							re.data.imageList.forEach(img => {
								_cc.push(img.url)
							})
							this.userInfo.avatar = _cc
						}
						this.content = re.data.memberCircleResps.rows

						this.content.forEach(re => {
							re.imageArray = re.image ? re.image.split(",") : [];

							let imgs = [];
							if (re.imgList) {
								re.imgList.forEach(nn => {
									imgs.push({
										src: nn
									})
								})
							}

							re.imgListData = imgs
						})

					} else if (re.code = 401) {
						this.turnLogin()
					}
				});
			},
			checkLogin(){
				if(!uni.getStorageSync("token")){
					this.turnLogin()
				}
			},
			turnLogin(){
				uni.removeStorageSync("token")
				uni.removeStorageSync("userInfo")
				uni.navigateTo({
					url: '/minePages/login',
				});
			},

			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 震动跳转
			navEdit(e) {
				wx.vibrateLong();
				uni.navigateTo({
					url: '/circlePages/edit'
				})
			},
			// 处理内容，给内容添加对应的标识信息
			initContentData() {
				this.content.forEach((item, index) => {
					// 是否需要隐藏内容
					item.hideContent = false
					// 显示所有内容
					item.showAllContent = false
					// 内容容器的实际高度
					item.contentContainerHeight = 0
					// 内容容器是否初始化完成
					item.contentContainerInit = false
					this.$set(this.content, index, item)
				})
			},
			// 获取内容容器的信息
			getContentRectInfo() {
				let contentRect = {}
				const query = uni.createSelectorQuery().in(this)
				// 筛选出存在内容的数据
				this.content.forEach((item, index) => {
					if (item?.content) {
						query.select(`#blogger__content--${index}`).boundingClientRect()
						contentRect[index] = item
					}
				})
				// 获取所有内容的宽高信息
				query.exec(res => {
					if (!res) {
						setTimeout(() => {
							this.getContentRectInfo()
						}, 10)
						return
					}
					// console.log(res);
					res.map((item) => {
						// console.log(item.height, this.contentHideShowHeight);
						// 获取对应的标号
						const id = item.id
						const idIndex = /blogger__content--(\d)/.exec(id)[1]
						let contentItem = this.content[idIndex]
						contentItem.hideContent = item.height > this.contentHideShowHeight
						contentItem.showAllContent = false
						contentItem.contentContainerHeight = item.height
						contentItem.contentContainerInit = true
						this.$set(this.content, idIndex, contentItem)

						// console.log(/blogger__content--(\d)/.exec(id)[1]);
					})
				})
			},
			// 切换内容的显示与隐藏
			switchContentShowStatus(index) {
				const contentItem = this.content[index]
				contentItem.showAllContent = !contentItem.showAllContent
				this.$set(this.content, index, contentItem)
			},


			// adSwiperChange(e) {
			//   // console.log(e);
			//   this.adSwiperCurrentIndex = e.detail.current
			// }
		}
	}
</script>

<style lang="scss" scoped>
	.template-blogger {}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	$cube-size: 180rpx;
	$cube-split: 90rpx;

	/* 立体头像 start*/

	.cube {
		background: #fff;
		background-repeat: no-repeat;
		background-size: cover;
		height: 350rpx;
		display: flex;
		justify-content: center;
		padding-top: 40rpx;
		overflow: hidden;
		position: relative;
		flex-direction: column;
		align-items: center;
		font-weight: 300;

		&__container {
			margin-top: 180rpx;
			position: relative;
			width: $cube-size;
			height: $cube-size;
			-webkit-perspective: 500px;
			perspective: 500px; //透视太大会超过屏幕就不好了吖

			&:before {
				content: '';
				width: $cube-size;
				height: $cube-size;
				position: absolute;
				// top: calc(50% - ($cube-split - 30px));
				// left: calc(50% - $cube-split);
				background-color: #3c6496;
				filter: blur(60px);
				opacity: .8;
			}

			&__body {
				position: absolute;
				width: 100%;
				height: 100%;
				transform-style: preserve-3d;
				transform: translateZ(-75px);
				animation: cubeFrame 35s cubic-bezier(0.36, -0.03, 0.46, 0.95) infinite alternate;
				will-change: transform;

				&__item {
					position: absolute;
					display: block;
					display: flex;
					align-items: center;
					justify-content: center;
					width: $cube-size;
					height: $cube-size;
					font-size: 30px;
					background-repeat: no-repeat;
					background-size: cover;

					&--front {
						transform: rotateY(0deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}

					&--back {
						transform: rotateX(180deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}

					&--right {
						transform: rotateY(90deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}

					&--left {
						transform: rotateY(-90deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}

					&--top {
						transform: rotateX(90deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}

					&--bottom {
						transform: rotateX(-90deg) translateZ($cube-split);
						background-color: #BEEBFF;
					}
				}
			}
		}
	}

	@keyframes cubeFrame {
		10% {
			transform: translateZ(-75px) rotateX(40deg) rotateY(60deg);
		}

		15% {
			transform: translateZ(-75px) rotateX(80deg) rotateY(20deg);
		}

		20% {
			transform: translateZ(-75px) rotateX(-180deg) rotateY(-70deg);
		}

		30% {
			transform: translateZ(-75px) rotateX(90deg) rotateY(180deg);
		}

		40% {
			transform: translateZ(-75px) rotateX(-10deg) rotateY(-140deg);
		}

		45% {
			transform: translateZ(-75px) rotateX(-100deg) rotateY(20deg);
		}

		55% {
			transform: translateZ(-75px) rotateX(-10deg) rotateY(-35deg);
		}

		60% {
			transform: translateZ(-75px) rotateX(180deg) rotateY(360deg);
		}

		70% {
			transform: translateZ(-75px) rotateX(-180deg) rotateY(-360deg);
		}

		80% {
			transform: translateZ(-75px) rotateX(45deg) rotateY(-70deg);
		}

		90% {
			transform: translateZ(-75px) rotateX(-45deg) rotateY(70deg);
		}

		100% {
			transform: translateZ(-75px) rotateX(-360deg) rotateY(360deg);
		}
	}

	/* 立体头像 end*/

	/* 信息提示 start */
	.blogger-tips-data {
		background-color: #F8F7F3;

		&__wrap {
			border-radius: 60rpx 60rpx 0 0;
		}

		&__message {
			padding-top: 60rpx;

			&__container {
				padding: 5rpx;
				border-radius: 100rpx;
			}

			&__avatar {
				margin: 6rpx 0 0 6rpx;
			}
		}

		&__info {
			padding: 40rpx 0 0 0;
		}
	}

	/* 信息提示 end */

	/* 文章内容 start*/
	.blogger {
		&__item {
			padding: 30rpx;
		}

		&__author {
			&__btn {
				margin-right: -12rpx;
				opacity: 0.5;
			}
		}

		&__desc {
			line-height: 55rpx;

			&__label {
				padding: 0 20rpx;
				margin: 0rpx 18rpx 0 0;

				&--prefix {
					color: #00FFC8;
					padding-right: 10rpx;
				}
			}

			&__content {}
		}

		&__content {
			margin-top: 18rpx;
			padding-right: 18rpx;

			&__data {
				line-height: 46rpx;
				text-align: justify;
				overflow: hidden;
				transition: all 0.25s ease-in-out;
			}

			&__status {
				margin-top: 10rpx;
				font-size: 26rpx;
				color: #82B2FF;
			}
		}

		&__main-image {
			border-radius: 16rpx;

			&--1 {
				max-width: 80%;
				max-height: 300rpx;
			}

			&--2 {
				max-width: 260rpx;
				max-height: 260rpx;
			}

			&--3 {
				height: 212rpx;
				width: 100%;
			}
		}

		&__count-icon {
			font-size: 40rpx;
			padding-right: 5rpx;
		}

		&__ad {
			width: 100%;
			height: 500rpx;
			transform: translate3d(0px, 0px, 0px) !important;

			::v-deep .uni-swiper-slide-frame {
				transform: translate3d(0px, 0px, 0px) !important;
			}

			.uni-swiper-slide-frame {
				transform: translate3d(0px, 0px, 0px) !important;
			}

			&__item {
				position: absolute;
				width: 100%;
				height: 100%;
				transform-origin: left center;
				transform: translate3d(100%, 0px, 0px) scale(1) !important;
				transition: transform 0.25s ease-in-out;
				z-index: 1;

				&--0 {
					transform: translate3d(0%, 0px, 0px) scale(1) !important;
					z-index: 4;
				}

				&--1 {
					transform: translate3d(13%, 0px, 0px) scale(0.9) !important;
					z-index: 3;
				}

				&--2 {
					transform: translate3d(26%, 0px, 0px) scale(0.8) !important;
					z-index: 2;
				}
			}

			&__content {
				border-radius: 40rpx;
				width: 640rpx;
				height: 500rpx;
				overflow: hidden;
			}

			&__image {
				width: 100%;
				height: 100%;
			}
		}
	}

	/* 文章内容 end*/

	/* 间隔线 start*/
	.tn-strip-bottom {
		width: 100%;
		border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/
	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/

	/* 广告内容 start */
	.ad-image {
		width: 80rpx;
		height: 80rpx;
		position: relative;
	}

	.ad-pic {
		background-size: cover;
		background-repeat: no-repeat;
		// background-attachment:fixed;
		background-position: top;
		border-radius: 20%;
	}

	/* 自定义导航栏内容 end */

	/* 悬浮 */
	.tnxuanfu {
		animation: suspension 3s ease-in-out infinite;
	}

	@keyframes suspension {

		0%,
		100% {
			transform: translateY(0);
		}

		50% {
			transform: translateY(-0.8rem);
		}
	}

	/* 悬浮按钮 */
	.button-shop {
		width: 90rpx;
		height: 90rpx;
		display: flex;
		flex-direction: row;
		position: fixed;
		/* bottom:200rpx;
      right: 20rpx; */
		left: 5rpx;
		top: 5rpx;
		z-index: 1001;
		border-radius: 100px;
		opacity: 0.9;
	}


	/* 按钮 */
	.edit {
		bottom: 300rpx;
		right: 75rpx;
		position: fixed;
		z-index: 9999;
	}


	.pa,
	.pa0 {
		position: absolute
	}

	.pa0 {
		left: 0;
		top: 0
	}


	.bg0 {
		width: 100rpx;
		height: 100rpx;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.bg1 {
		width: 100%;
		height: 100%;
	}




	.hx-box {
		top: 50%;
		left: 50%;
		width: 100rpx;
		height: 100rpx;
		transform-style: preserve-3d;
		transform: translate(-50%, -50%) rotateY(75deg) rotateZ(10deg);
	}

	.hx-box .pr {
		width: 100rpx;
		height: 100rpx;
		transform-style: preserve-3d;
		animation: hxz 20s linear infinite;
	}

	@keyframes hxz {
		0% {
			transform: rotateX(0deg);
		}

		100% {
			transform: rotateX(-360deg);
		}
	}



	.hx-box .pr .pa0 {
		width: 100rpx;
		height: 100rpx;
		/* border: 4px solid #5ec0ff; */
		border-radius: 1000px;
	}

	.hx-box .pr .pa0 .span {
		display: block;
		width: 100%;
		height: 100%;
		background: url(https://resource.tuniaokj.com/images/cool_bg_image/arc4.png) no-repeat center center;
		background-size: 100% 100%;
		animation: hx 4s linear infinite;
	}

	@keyframes hx {
		to {
			transform: rotate(360deg);
		}
	}

	.hx-k1 {
		transform: rotateX(-60deg) rotateZ(-60deg)
	}

	.hx-k2 {
		transform: rotateX(-30deg) rotateZ(-30deg)
	}

	.hx-k3 {
		transform: rotateX(0deg) rotateZ(0deg)
	}

	.hx-k4 {
		transform: rotateX(30deg) rotateZ(30deg)
	}

	.hx-k5 {
		transform: rotateX(60deg) rotateZ(60deg)
	}

	.hx-k6 {
		transform: rotateX(90deg) rotateZ(90deg)
	}
</style>
